<template>
	<view class="index">
		<block v-if="list.length == 0">
			<view class="no-data">你还没有添加收货地址</view>
			</block>
			<block v-else>
				<view class="address" v-for="(item, index) in list" :key="index">
					<view class="header" @tap="select_address(item)">
						<view class="select" v-if="type == 'select'">
							<image :src="item.id == address_id ? '/static/icon/selected.png' : '/static/icon/she.png'"></image>
						</view>
						<view class="iconfont icon-dizhi-01" v-else></view>
						
						<view class="info">
							<view class="top">
								<view>{{item.recipients}}</view>
								<view>{{item.mobile}}</view>
							</view>
							<view class="content">{{item.province}} {{item.city}} {{item.area}} {{item.address}}</view>
						</view>
					</view>
					<view class="footer" v-if="type != 'select'">
						<view class="default" @tap="set_default(item)">
							<image :src="item.is_default==1?'/static/icon/selected.png':'/static/icon/she.png'"></image> 默认地址
						</view>

						<view class="right">
							<view class="btn" @tap="edit(item)">
								<view class="iconfont icon-yijianfankui"></view>
								<view class="name">编辑</view>
							</view>
							<view class="btn" @tap="del(item, index)">
								<view class="iconfont icon-shanchu"></view>
								<view class="name">删除</view>
							</view>
						</view>
					</view>
				</view>
				</block>
				<navigator class="add" url="edit">添加新地址</navigator>
	</view>
</template>

<script>
	import Request from '../../common/request.js';
	
	export default {
		data() {
			return {
				list: [],
				type: '',
				address_id : ''
			};
		},
		onLoad(options) {
			this.type = options.type?options.type:'';
			this.address_id = uni.getStorageSync('address_id');
			
		},
		onShow() {
			this.getList();
		},
		methods: {
			getList: function(){
				var that = this;
				var params = {
					url: 'address/index',
					sCallback: function(res) {
						that.list = res.data;
					}
				}
				
				Request.send(params);
			},
			edit: function(item){
				var url = '/pages/address/edit';
					url += '?id='+item.id+'&recipients='+item.recipients+'&mobile='+item.mobile;
					url += '&address='+item.address+'&province='+item.province+'&city='+item.city;
					url += '&area='+item.area+'&province_id='+item.province_id+'&city_id='+item.city_id;
					url += '&area_id='+item.area_id;
				
				uni.navigateTo({
					url: url
				})
			},
			del: function(item, index){
				var that = this;
				uni.showModal({
					title:'提示',
					content: '确定删除该收货地址吗？',
					success:function(e){
						if(e.confirm) {
							var params = {
								url: 'address/delAddress',
								data: {id: item.id},
								sCallback: function(e) {
									that.getList();
								}
							}
							Request.send(params);
						}
					}
				})
				
				
			},
			set_default: function(item){
				var that = this;
				var postData = item;
				postData.is_default = 1;
				var params = {
					url: 'address/editAddress',
					data: postData,
					sCallback: function() {
						that.getList();
					}
				}
				
				Request.send(params);
			},
			//从结算页面过来选择收货地址
			select_address: function(item){
				if(this.type == 'select') {
					uni.setStorageSync('address_id', item.id)
					uni.navigateBack({
						
					})
				}
			}
			
		}
	}
</script>

<style>
	.index{
		margin-bottom: 80upx;
		font-size: 28upx;
	}

	.index .add{
		height: 80upx;
		line-height: 80upx;
		background: #e60012;
		color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		text-align: center;
	}

	.index .address{
		background: #fff;
		margin-top: 20upx;
	}

	.index .address .header{
		display: flex;
		width: 100%;
		align-items: center;
		color: #666;
		border-bottom: 1upx solid #eee;
		padding: 20upx 0;
	}
	
	.index .address .select image{
		width: 40upx;
		height: 40upx;
		margin-right: 10upx;
	}

	.index .address .select, .index .address .header .iconfont{
		width: 80upx;
		display: flex;
		justify-content: center;
		font-size: 40upx;
		color: #666;
	}

	.index .address .header .info{
		flex: 1;
		padding: 0 20upx;
	}

	.index .address .header .top{
		display: flex;
		justify-content: space-between;
	}

	.index .address .footer{
		height: 80upx;
		display: flex;
		align-items: center;
		width: 100%;
		color: #999;
	}

	.index .address .footer image{
		width: 40upx;
		height: 40upx;
		margin-right: 10upx;
	}

	.index .address .footer .default{
		display: flex;
		align-items: center;
		padding-left: 20upx;
	}

	.index .address .footer .right{
		flex: 1;
		display: flex;
		padding-right: 20upx;
		justify-content: flex-end;
	}

	.index .address .footer .right .btn{
		display: flex;
		margin-left: 40upx;
	}

	.index .address .footer .right .iconfont{
		margin-right: 10upx;
	}
</style>
